package com.ruoyi.workorder.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.wbs.domain.ProjectActivity;
import com.ruoyi.wbs.service.IProjectActivityService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.workorder.domain.WorkOrder;
import com.ruoyi.workorder.service.IWorkOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工单Controller
 * 
 * @author renyuxin
 * @date 2024-05-28
 */
@RestController
@RequestMapping("/workorder/workorder")
public class WorkOrderController extends BaseController
{
    @Autowired
    private IWorkOrderService workOrderService;

    @Autowired
    private IProjectActivityService projectActivityService;

    /**
     * 查询工单列表
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorder:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkOrder workOrder)
    {
        startPage();
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = loginUser.getUserId();
//        List<WorkOrder> list = workOrderService.selectWorkOrderList(workOrder);
        List<WorkOrder> list = workOrderService.selectWorkOrderListForUser(workOrder, userId);
        return getDataTable(list);
    }

    /**
     * 导出工单列表
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorder:export')")
    @Log(title = "工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkOrder workOrder)
    {
        List<WorkOrder> list = workOrderService.selectWorkOrderList(workOrder);
        ExcelUtil<WorkOrder> util = new ExcelUtil<WorkOrder>(WorkOrder.class);
        util.exportExcel(response, list, "工单数据");
    }

    /**
     * 获取工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorder:query')")
    @GetMapping(value = "/{woId}")
    public AjaxResult getInfo(@PathVariable("woId") Long woId)
    {
        return success(workOrderService.selectWorkOrderByWoId(woId));
    }

    /**
     * 新增工单
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorder:add')")
    @Log(title = "工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkOrder workOrder)
    {
        //唯一性验证
        if(workOrderService.checkNameUnique(workOrder)){
            return error("标题已存在,请换一个!");
        }
        return toAjax(workOrderService.insertWorkOrder(workOrder));
    }

    /**
     * 修改工单
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorder:edit')")
    @Log(title = "工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkOrder workOrder)
    {
        //唯一性验证
        if(workOrderService.checkNameUnique(workOrder)){
            return error("标题已存在,请换一个!");
        }
        return toAjax(workOrderService.updateWorkOrder(workOrder));
    }

    /**
     * 删除工单
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorder:remove')")
    @Log(title = "工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{woIds}")
    public AjaxResult remove(@PathVariable Long[] woIds)
    {
        return toAjax(workOrderService.deleteWorkOrderByWoIds(woIds));
    }
    @GetMapping("/getAct/{projectId}")
    public AjaxResult getAct(@PathVariable("projectId") Long projectId)
    {

        List<ProjectActivity> projectActivityList = projectActivityService.selectActByProjectId(projectId);
        return success(projectActivityList);
    }

}
